package com.hmyd.hmyd_android.view;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.AbstractWheelTextAdapter;
import com.hmyd.hmyd_android.adapter.ArrayWheelAdapter;
import com.hmyd.hmyd_android.weigets.OnWheelChangedListener;
import com.hmyd.hmyd_android.weigets.OnWheelScrollListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;


public class WheelPopupwindow extends PopupWindow implements OnClickListener {
	// Scrolling flag
	private boolean scrolling = false;
	
	private View mainView;
	private View cancel;
	private View ok;
	
	private Activity context;
	private WheelPopupListener mWheelPopupListener;
	
	WheelView country;
	WheelView city;
	
	public String countries[];
	public String cities[][];
	
	public WheelPopupwindow(Activity paramActivity, WheelPopupListener wheelPopupListener, int paramInt1, int paramInt2){  
		super(paramActivity);  
		
		context = paramActivity;
		
		mainView = LayoutInflater.from(paramActivity).inflate(R.layout.wheel_popup_window, null);  
		
		initViews(mainView);
		
		//设置每个子布局的事件监听器  
		mWheelPopupListener = wheelPopupListener;
		
		setContentView(mainView);  
		
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		
		//设置宽度  
		/*setWidth(paramInt1);  
		//设置高度  
		setHeight(paramInt2); */ 
		//设置显示隐藏动画  
		setAnimationStyle(R.style.AnimTools);  
		//设置背景透明  
		setBackgroundDrawable(new ColorDrawable(0));  
   }
	
	private void initViews(View root) {
		country = (WheelView) root.findViewById(R.id.country);
		country.setVisibleItems(3);
		country.setViewAdapter(new CountryAdapter(context));

		cities = new String[][] {
				new String[] {"北京市"},
				new String[] {"上海市"},
				new String[] {"安庆市", "蚌埠市", "巢湖市", "池州市", "滁州市", "阜阳市", "合肥市", "淮北市", "淮南市", "黄山市", "六安市", "马鞍山市", "宿州市", "铜陵市", "芜湖市", "宣城市", "亳州市"},
				new String[] {"重庆市", "涪陵市","黔江市","万州市"},
				new String[] {"福州市", "龙岩市","南平市","宁德市","莆田市","泉州市","三明市","厦门市","漳州市"},
				new String[] {"潮州市", "东莞市", "佛山市", "广州市", "河源市", "惠州市", "江门市", "揭阳市", "茂名市", "梅州市", "清远市", "汕头市", "汕尾市", "韶关市", "深圳市", "阳江市", "云浮市", "湛江市", "肇庆市", "中山市", "珠海市"},
				new String[] {"安顺市", "毕节地区", "贵阳市", "六盘水市", "黔东南苗族侗族自治州", "黔南布依族苗族自治州", "黔西南布依族苗族自治州", "铜仁地区", "遵义市"},
				new String[] {"白银市", "定西市", "甘南藏族自治州", "嘉峪关市", "金昌市", "酒泉市", "兰州市", "临夏回族自治州", "陇南市", "平凉市", "庆阳市", "天水市", "武威市", "张掖市"},
				new String[] {"百色市", "北海市", "崇左市", "防城港市", "桂林市", "贵港市", "河池市", "贺州市", "来宾市", "柳州市", "南宁市", "钦州市", "梧州市", "玉林市"},
				new String[] {"保定市", "沧州市", "承德市", "邯郸市", "衡水市", "廊坊市", "秦皇岛市", "石家庄市", "唐山市", "邢台市", "张家口市"},
				new String[] {"安阳市", "鹤壁市", "焦作市", "开封市", "洛阳市", "南阳市", "平顶山市", "三门峡市", "商丘市", "新乡市", "信阳市", "许昌市", "郑州市", "周口市", "驻马店市", "漯河市", "濮阳市"},
				new String[] {"大庆市", "大兴安岭地区", "哈尔滨市", "鹤岗市", "黑河市", "鸡西市", "佳木斯市", "牡丹江市", "七台河市", "齐齐哈尔市", "双鸭山市", "绥化市", "伊春市"},
				new String[] {"海口市", "三亚市"},
				new String[] {"鄂州市", "恩施土家族苗族自治州", "黄冈市", "黄石市", "荆门市", "荆州市", "十堰市", "随州市", "武汉市", "咸宁市", "襄樊市", "孝感市", "宜昌市"},
				new String[] {"常德市", "长沙市", "郴州市", "衡阳市", "怀化市", "吉首市", "娄底市", "邵阳市", "湘潭市", "益阳市", "永州市", "岳阳市", "张家界市", "株州市"},
				new String[] {"常州市", "淮安市", "连云港市", "南京市", "南通市", "苏州市", "宿迁市", "泰州市", "无锡市", "徐州市", "盐城市", "扬州市", "镇江市"},
				new String[] {"白城市", "白山市", "长春市", "吉林市", "辽源市", "四平市", "松原市", "通化市", "延边朝鲜族自治州"},
				new String[] {"抚州市", "赣州市", "吉安市", "景德镇市", "九江市", "南昌市", "萍乡市", "上饶市", "新余市", "宜春市", "鹰潭市"},
				new String[] {"鞍山市", "本溪市", "朝阳市", "大连市", "丹东市", "抚顺市", "阜新市", "葫芦岛市", "锦州市", "辽阳市", "盘锦市", "沈阳市", "铁岭市", "营口市"},
				new String[] {"阿拉善盟", "巴彦淖尔市", "包头市", "赤峰市", "鄂尔多斯市", "呼和浩特市", "呼伦贝尔市", "通辽市", "乌海市", "乌兰察布市", "锡林郭勒盟", "兴安盟"},
				new String[] {"固原市", "石嘴山市", "吴忠市", "银川市", "中卫市"},
				new String[] {"果洛藏族自治州", "海北藏族自治州", "海东地区", "海南藏族自治州", "海西蒙古族藏族自治州", "黄南藏族自治州", "西宁市", "玉树藏族自治州"},
				new String[] {"滨州市", "德州市", "东营市", "菏泽市", "济南市", "济宁市", "莱芜市", "聊城市", "临沂市", "青岛市", "日照市", "泰安市", "威海市", "潍坊市", "烟台市", "枣庄市", "淄博市"},
				new String[] {"长治市", "大同市", "晋城市", "晋中市", "临汾市", "吕梁市", "朔州市", "太原市", "忻州市", "阳泉市", "运城市"},
				new String[] {"安康市", "宝鸡市", "汉中市", "商洛市", "铜川市", "渭南市", "西安市", "咸阳市", "延安市", "榆林市"},
				new String[] {"阿坝藏族羌族自治州", "巴中市", "成都市", "达州市", "德阳市", "甘孜藏族自治州", "广安市", "广元市", "乐山市", "凉山彝族自治州", "眉山市", "绵阳市", "南充市", "内江市", "攀枝花市", "遂宁市", "雅安市", "宜宾市", "资阳市", "自贡市", "泸州市"},
				new String[] {"天津市"},
				new String[] {"阿里地区", "昌都地区", "拉萨市", "林芝地区", "那曲地区", "日喀则地区", "山南地区", "樟木口岸镇"},
				new String[] {"阿克苏地区", "阿拉尔市", "阿勒泰地区", "巴音郭楞蒙古族自治州", "博尔塔拉蒙古族自治州", "昌吉回族自治州", "哈密地区", "和田地区", "喀什地区", "克拉玛依市", "克孜勒苏柯尔克孜自治州", "石河子市", "塔城地区", "图木舒克市", "吐鲁番地区", "乌鲁木齐市", "伊犁哈萨克自治州"},
				new String[] {"保山市", "楚雄彝族自治州", "大理白族自治州", "德宏傣族景颇族自治州", "迪庆藏族自治州", "红河哈尼族彝族自治州", "昆明市", "丽江市", "临沧市", "怒江傈僳族自治州", "普洱市", "曲靖市", "文山壮族苗族自治州", "西双版纳傣族自治州", "玉溪市", "昭通市"},
				new String[] {"杭州市", "湖州市", "嘉兴市", "金华市", "丽水市", "宁波市", "绍兴市", "台州市", "温州市", "舟山市", "衢州市"},
		};
		
		countries = new String[] {"北京市", "上海市", "安徽省", "重庆市","福建省",
				"广东省","贵州省","甘肃省","广西自治区",
				"河北省","河南省","黑龙江省","海南省",
				"湖北省","湖南省","江苏省","吉林省",
				"江西省","辽宁省","内蒙古自治区","宁夏自治区",
				"青海省","山东省","山西省","陕西省",
				"四川省","天津市","西藏自治区","新疆自治区",
				"云南省","浙江省"};

		city = (WheelView) root.findViewById(R.id.city);
		city.setVisibleItems(5);

		country.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!scrolling) {
					updateCities(city, cities, newValue);
				}
			}
		});

		country.addScrollingListener( new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}
			@Override
			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				updateCities(city, cities, country.getCurrentItem());
			}
		});

		country.setCurrentItem(1);
		
		cancel = mainView.findViewById(R.id.cancel);
		ok = mainView.findViewById(R.id.ok);
		
		cancel.setOnClickListener(this);  
		ok.setOnClickListener(this); 
	}
	
	/**
	 * Updates the city wheel
	 */
	private void updateCities(WheelView city, String cities[][], int index) {
		ArrayWheelAdapter<String> adapter =
				new ArrayWheelAdapter<String>(context, cities[index]);
		adapter.setTextSize(18);
		city.setViewAdapter(adapter);
		city.setCurrentItem(cities[index].length / 2);
	}

	/**
	 * Adapter for countries
	 */
	private class CountryAdapter extends AbstractWheelTextAdapter {
		// Countries names
		/**
		 * Constructor
		 */
		protected CountryAdapter(Context context) {
			super(context, R.layout.view_wheel_country_layout, NO_RESOURCE);

			setItemTextResource(R.id.country_name);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
//			ImageView img = (ImageView) view.findViewById(R.id.flag);
//			img.setImageResource(flags[index]);
			return view;
		}

		@Override
		public int getItemsCount() {
			return countries.length;
		}

		@Override
		protected CharSequence getItemText(int index) {
			return countries[index];
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			dismiss();
			break;
		case R.id.ok:
			mWheelPopupListener.getFirstString(countries[country.getCurrentItem()]);
			mWheelPopupListener.getSecondString(cities[country.getCurrentItem()][city.getCurrentItem()]);
			
			dismiss();
			break;
		default:
			break;
		}
	}
}
